package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.NewsDto;
import primetest.impl.dao.NewsDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.NewsService;

public class NewsService implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -2425578018416960291L;
	/**
	 *
	 */

	private Transaction transaction;
	private NewsDao newsDao;

	@Inject
	public NewsService(Transaction transaction,NewsDao newsDao){
		this.transaction = transaction;
		this.newsDao = newsDao;
	}

	public List<NewsDto> getNewsList() throws IOException {
		try{
			transaction.begin();
			List<NewsDto> newsList = newsDao.getAllList(transaction);
			//transaction.commit();
			return newsList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}



	public void addNews(NewsDto newsDto) throws IOException {
		try{
			transaction.begin();
			newsDao.addNews(transaction, newsDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateNews(NewsDto newsDto) throws IOException {
		try{
			transaction.begin();
			newsDao.updateNews(transaction, newsDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void deleteNews(String newsSeq) throws IOException {
		try{
			transaction.begin();
			newsDao.deleteNews(transaction, newsSeq);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}
}
